#ifndef _XP_LIST_H
#define _XP_LIST_H

struct xp_slist_node
{
    struct xp_slist_node *next;
};

struct xp_slist_handler
{
    struct xp_slist_node *head;
    struct xp_slist_node *tail;
    int length;
};

extern void xp_slist_init(struct xp_slist_handler *handler);
extern void xp_slist_add(struct xp_slist_handler *handler, struct xp_slist_node *node);
extern void xp_slist_add_tail(struct xp_slist_handler *handler, struct xp_slist_node *node);
extern void xp_slist_del(struct xp_slist_handler *handler, struct xp_slist_node *node);
#define xp_slist_foreach(handler, node) for (((struct xp_slist_node *)(node)) = ((struct xp_slist_handler *)(handler))->head; \
                                             ((struct xp_slist_node *)(node)) != NULL;                                        \
                                             ((struct xp_slist_node *)(node)) = ((struct xp_slist_node *)(node))->next)

#if 0
struct xp_dlist_head {

};

struct xp_dlist_node {

};

void xp_dlist_add(xp_dlist_head *head, xp_dlist_node *node);
void xp_dlist_add_tail(xp_dlist_head *head, xp_dlist_node *node);
void xp_dlist_del(xp_dlist_head *head, xp_dlist_node *node);
#define xp_dlist_foreach(head, node) for (((xp_dlist_node *)(head)) = ((xp_dlist_head *)(head))->next; \
                                          ((xp_dlist_node *)(node)) != NULL;                           \
                                          ((xp_dlist_node *)(node)) = ((xp_dlist_node *)(node))->next)
#endif

#if 0
struct xp_table_row
{
    unsigned int index;
    unsigned char *key; // 如果需要使用字符串的方式去查询数据，将payload中包含字符串的字段赋值到这里，否则为空
    void *payload;
};

struct xp_table_handler
{
    unsigned int rows;
    unsigned int tsize;
    int key_offset; // 小于0表示
    struct xp_table_row *tbody;
};

extern void xp_table_create(struct xp_table_handler *handler, struct xp_table_row *tbody, int length);
int xp_table_insert(struct xp_table_handler *handler, unsigned int id, void *payload);
int xp_table_delete(struct xp_table_handler *handler, unsigned int id);
int xp_table_select(struct xp_table_handler *handler, unsigned int id, void **payload);
int xp_table_update(struct xp_table_handler *handler, unsigned int id, void *payload);

void xp_table_create_with_key(struct xp_table_handler *handler, struct xp_table_row *tbody, int length, int key_offset);
int xp_table_insert_by_key(struct xp_table_handler *handler, unsigned int id, void *payload);
int xp_table_delete_by_key(struct xp_table_handler *handler, unsigned char *key);
int xp_table_select_by_key(struct xp_table_handler *handler, unsigned char *key, void **payload);
int xp_table_update_by_key(struct xp_table_handler *handler, unsigned char *key, void *payload);

int xp_table_sortby_index(struct xp_table_handler *handler);
int xp_table_sortby_index_desc(struct xp_table_handler *handler);
#endif

#endif
